/**
 * 
 */
package cutetd.db;

import java.util.*;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import cutetd.model.M_Navi;

/**
 * @author merlyle
 * 
 */
public class GaeDbBase {
	private PersistenceManager pm;

	/**主键为string 类型时，可直接用
	 * @param o
	 */
	public void saveOrUpdate(Object o) {
		pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(o);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			pm.close();
		}
	}
	
	/**主键为long
	 * @param o
	 */
	public void update(Object o) {
		pm = PMF.get().getPersistenceManager();
		try {
		//	getObjectById(o, id)
			pm.makePersistent(o);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			pm.close();
		}
	}

	public Object saveGetObject(Object o) {
		Object getO = null;
		pm = PMF.get().getPersistenceManager();
		try {
			Object tmp = pm.makePersistent(o);
			getO = pm.detachCopy(tmp);
		} catch (Exception e) {
			System.out.println("saveGetObject" + e.getMessage());
		} finally {
			pm.close();
		}
		return getO;
	}

	public Object getObjectById(Class<?> c, Long id) {
		Object o = null;
		pm = PMF.get().getPersistenceManager();
		try {
			Object tmp = pm.getObjectById(c, id);
			o = pm.detachCopy(tmp);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			pm.close();
		}
		return o;
	}

	public Object getObjectByKey(Class<?> c, Object k) {
		Object o = null;
		pm = PMF.get().getPersistenceManager();

		try {
			Object tmp = pm.getObjectById(c, k);
			o = pm.detachCopy(tmp);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			pm.close();
		}
		return o;
	}
	
	@SuppressWarnings("unchecked")
	public List<?> getObjectByKeyList(Class<?> c, List<?> keys) {
		pm = PMF.get().getPersistenceManager();
		
		@SuppressWarnings("rawtypes")
		List list = new ArrayList();
		
		try {
			int i = 0;
			while (i < keys.size()) {
				list.add(pm.getObjectById(c, keys.get(i)));		
				++i;
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			pm.close();
		}
		return list;
	}

	public void delete(Object o) {
		pm = PMF.get().getPersistenceManager();
		try {
			pm.deletePersistent(o);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			pm.close();
		}
	}

	public List<?> getListAll(Class<?> c) {
		pm = PMF.get().getPersistenceManager();

		Query query = pm.newQuery(c);
		List<?> results = null;
		try {
			List<?> tmp = (List<?>) query.execute();
			results = (List<?>) pm.detachCopyAll(tmp);
		} finally {
			query.closeAll();
			pm.close();
		}
		return results;
	}
	
	/**
	 * @param c
	 * @param sort	排序参数
	 * @param order asc/desc
	 * @return
	 */
	public List<?> getListAllByOrder(Class<?> c, String sort, String order) {
		pm = PMF.get().getPersistenceManager();

		Query query = pm.newQuery(M_Navi.class);
		query.setOrdering(sort + " " + order);
		List<?> results = null;
		try {
			List<?> tmp = (List<?>) query.execute();
			results = (List<?>) pm.detachCopyAll(tmp);
		} finally {
			query.closeAll();
			pm.close();
		}
		return results;
	}

	/**
	 * 根据字符串查询匹配的对象列表
	 * 
	 * @param c
	 *            查询的表对象
	 * @param filter
	 *            查询的对象属性
	 * @param param
	 *            查询的变量值
	 * @param orderBy
	 *            查询结果排序
	 * @return
	 */
	public List<?> getListMateString(Class<?> c, String filter, String param,
			String orderBy) {
		pm = PMF.get().getPersistenceManager();

		Query query = pm.newQuery(c);
		query.setFilter(filter + " == inputParam");
		query.setOrdering(orderBy);
		query.declareParameters("String inputParam");

		List<?> results = null;
		try {
			List<?> tmp = (List<?>) query.execute(param);
			results = (List<?>) pm.detachCopyAll(tmp);
		} finally {
			query.closeAll();
			pm.close();
		}
		return results;
	}

	/**
	 * 根据长整形查询匹配的对象列表
	 * 
	 * @param c
	 *            查询的表对象
	 * @param filter
	 *            查询的对象属性
	 * @param param
	 *            查询的变量值
	 * @param orderBy
	 *            查询结果排序
	 * @return
	 */
	public List<?> getListMateLong(Class<?> c, String filter, Long param,
			String orderBy) {
		getListAll(c);
		pm = PMF.get().getPersistenceManager();

		Query query = pm.newQuery(c);
		query.setFilter(filter + " == inputParam");
		query.setOrdering(orderBy);
		query.declareParameters("Long inputParam");

		List<?> results = null;
		try {
			List<?> tmp = (List<?>) query.execute(param);
			results = (List<?>) pm.detachCopyAll(tmp);
		} finally {
			query.closeAll();
			pm.close();
		}
		return results;
	}

	/**
	 * 根据查询语句获取对象列表
	 * 
	 * @param c
	 *            要查询的表对象
	 * @param whereOrderBy
	 *            查询语句，如："lastName == 'Smith' order by hireDate desc"
	 * @return
	 */
	public List<?> getListWhereOrderBy(Class<?> c, String where, String orderBy) {
		pm = PMF.get().getPersistenceManager();

		Query query = pm.newQuery(c, where);
		query.setOrdering(orderBy);

		List<?> results = null;
		try {
			List<?> tmp = (List<?>) query.execute();
			results = (List<?>) pm.detachCopyAll(tmp);
		} finally {
			query.closeAll();
			pm.close();
		}
		return results;
	}
	
	/**
	 * @param c
	 * @param start 开始条数
	 * @param num   查询几条
	 * @param condition	查询条件
	 * @param order	排序: desc asc
	 * @return
	 */
	public List<?> getSubList(Class<?> c, int start, int num, String condition, String order) {
		pm = PMF.get().getPersistenceManager();
		
		Query query = pm.newQuery(c, condition);
		query.setOrdering(order);
		query.setRange(start, num);
		
		List<?> results = null;
		try {
			List<?> tmp = (List<?>) query.execute();
			results = (List<?>) pm.detachCopyAll(tmp);
		} finally {
			query.closeAll();
			pm.close();
		}
		return results;
	}
	
	public int getTotalNum(Class<?> c) {
		pm = PMF.get().getPersistenceManager();

		Query query = pm.newQuery(c);
		query.setResult("count(this)");
		int count;
		try {
			count = (Integer) query.execute();
		} finally {
			query.closeAll();
			pm.close();
		}
		return count;
	}
	
	public int getTotalNumByCondition(Class<?> c, String where) {
		pm = PMF.get().getPersistenceManager();

		Query query = pm.newQuery(c, where);
		query.setResult("count(this)");
		int count;
		try {
			count = (Integer) query.execute();
		} finally {
			query.closeAll();
			pm.close();
		}
		return count;
	}
}
